#include "pch.h"
#include "seBleedingParticle.h"
#include <time.h>

#define ONE_SIXTIETH 1/60.0f

sf::Image seBleedingParticle::mImage;

seBleedingParticle::seBleedingParticle()
{
}

void seBleedingParticle::Load()
{
	mImage.LoadFromFile("data/blood-particle.png");
}

void seBleedingParticle::Unload()
{

}

void seBleedingParticle::Init()
{
	mParticles.clear();

	mStopBleeding=false;
	mPlugged=true;
	mBleedingFreq=0;
	mTimer=4.0f;
	mScale=1.0f;
}

void seBleedingParticle::Step()
{
	mTimer += ONE_SIXTIETH;
	RefreshFrequency();
	ChangeScale();

	if(!mStopBleeding && mTimer >1/mBleedingFreq)
	{	
		mParticles.push_back(sf::Sprite(mImage));
		mParticles.back().SetPosition(mPosition.x-3+rand()%6,mPosition.y);
		mParticles.back().SetCenter(mParticles.back().GetSize().x / 2,mParticles.back().GetSize().y / 2);
		mParticles.back().SetScale(sf::Vector2f(mScale,mScale));
		mTimer=0.0f;
	}
	

	for (unsigned int i=0;i<mParticles.size();++i)
		mParticles[i].SetPosition(mParticles[i].GetPosition().x,mParticles[i].GetPosition().y+2);
	
	for (unsigned int i=0;i<mParticles.size();++i)
		if (mParticles[i].GetPosition().y > viewHeight + 20)
		{
			mParticles.erase(mParticles.begin() + i);
			i--;
		}
}

void seBleedingParticle::Draw()
{
	for(unsigned int i = 0; i < mParticles.size(); ++i)
	{
		app.Draw(mParticles[i]);
	}
}

void seBleedingParticle::SetPosition(sf::Vector2f position)
{
	mPosition=position;
}

void seBleedingParticle::SetStopBleeding(bool stopBleeding)
{
	mStopBleeding=stopBleeding;
}

void seBleedingParticle::SetPlugged(bool plugged)
{
	mPlugged=plugged;
}


void seBleedingParticle::RefreshFrequency()
{
	if (mPlugged)
	{
		if (mBleedingFreq>0)
			if(mBleedingFreq<0.6)
				mBleedingFreq-=1*ONE_SIXTIETH;
			else mBleedingFreq-=50*ONE_SIXTIETH/4;
		else
			mBleedingFreq=0;
	}
	else 
	{
		if (mBleedingFreq<50)
			//if(mBleedingFreq>23)
			mBleedingFreq += 50*ONE_SIXTIETH/2;
			//else mBleedingFreq+=25*ONE_SIXTIETH/4;
		else
			mBleedingFreq=25;
	}
}

void seBleedingParticle::ChangeScale()
{
	if (mPlugged)
	{	
		if (mScale>1.0f)
		mScale-=mScale*ONE_SIXTIETH/3;
		else
			mScale=1.0f;
	}
	else 
	{
		if (mScale<3.0f)
			mScale += mScale * ONE_SIXTIETH / 1.5f;
		else
			mScale=3.0f;
	}
}